#include "qipan.h"
#include <QDebug>
#include <QPoint>


/*1.开始游戏：不开始没有悬停事件
  2.倒计时 总时间
  3.悔棋
  4.联机
  5.游戏结束之后不能再对棋盘进行操作*/
qipan::qipan(QWidget *parent) : QWidget(parent)
{
    m_hoverFlag = false;
    /*清除脏数据*/
//    memset(m_board,0,sizeof (m_board));
    m_board = (Role **)malloc(sizeof(Role*)*m_boardSize);
    for(int idx = 0;idx < m_boardSize;idx++)
    {
        m_board[idx] = (Role*)malloc(sizeof(Role)*m_boardSize);
    }
    for(int row = 0;row <m_boardSize;row++)
    {
        for(int col = 0;col < m_boardSize;col++)
        {
            m_board[row][col] = EMPTY;
        }
    }
    /*捕获鼠标*/
    setMouseTracking(true);
}

void qipan::setBoardInfo(int row, int col, Role role)
{
    if((row >= 0 && row < m_boardSize) && (col >= 0 && col < m_boardSize) && (m_board[row][col] == EMPTY))
    {
        m_board[row][col] = role;
    }
}

Role **qipan::getBoardInfo(int &boardSize)
{
    boardSize = m_boardSize;
    return reinterpret_cast<Role**>(m_board);
}

/*获取棋盘尺寸*/
int qipan::getBoardSize()
{
    return m_boardSize;
}

void qipan::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    /*启用抗锯齿效果*/
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制棋盘背景
    // 棋盘背景颜色
    QColor backgroundColor = QColor(211, 141, 206); // 淡灰色
    painter.fillRect(rect(), backgroundColor);

    /*绘制棋盘的网格线*/
    for(int idx = 0;idx < m_boardSize;idx++)
    {
        /*绘制竖线*/
        painter.drawLine(m_margin + idx * m_cellSize,m_margin,m_margin + idx * m_cellSize,m_margin + (m_boardSize-1) * m_cellSize);
        /*绘制横线*/
        painter.drawLine(m_margin,m_margin + idx * m_cellSize,m_margin + (m_boardSize-1) * m_cellSize,m_margin + idx * m_cellSize);
    }
    /*绘制悬停点*/
    if(m_hoverFlag &&
        (m_hoverRow >= 0 && m_hoverRow < m_boardSize) &&
        (m_hoverCol >= 0 && m_hoverCol < m_boardSize) &&
        (m_board[m_hoverRow][m_hoverCol] == EMPTY))
    {
        painter.setBrush(Qt::red);
        painter.drawEllipse(QPoint(m_margin + m_hoverRow * m_cellSize,m_margin + m_hoverCol * m_cellSize),m_cellSize / 7,m_cellSize / 7);
    }

    /*绘制棋子*/
    for(int row = 0;row < m_boardSize;row++)
    {
        for(int col = 0;col < m_boardSize;col++)
        {
           if(m_board[row][col] == PLAYER)
           {
               painter.setBrush(Qt::black);
               painter.drawEllipse(QPoint(m_margin + row * m_cellSize,m_margin + col * m_cellSize),m_cellSize / 3,m_cellSize / 3);
           }
           else if(m_board[row][col] == COMPUTER)
           {
               painter.setBrush(Qt::white);
               painter.drawEllipse(QPoint(m_margin + row * m_cellSize,m_margin + col * m_cellSize),m_cellSize / 3,m_cellSize / 3);
           }
        }
    }
}

void qipan::mouseMoveEvent(QMouseEvent *event)
{

    /*找出每个交叉点坐标*/
    int row = (event->x()- m_margin + m_cellSize / 2)/m_cellSize;  // + m_cellSize / 2 防止向下取整导致误差
    int col = (event->y()- m_margin + m_cellSize / 2)/m_cellSize;

    /*判断鼠标是否在棋盘内*/
    if((row >= 0 && row < m_boardSize) && (col >= 0 && col < m_boardSize))
    {
        if(m_hoverCol != col || m_hoverRow != row)
        {
           m_hoverRow = row;
           m_hoverCol = col;
           m_hoverFlag = true;
           update();
        }
    }
    else
    {
        if(m_hoverFlag)
        {
            m_hoverRow = false;
            /*移除棋盘将坐标清空*/
            m_hoverCol = -1;
            m_hoverRow = -1;
            /*更新画面*/
            update();
        }
    }
}

void qipan::mousePressEvent(QMouseEvent *event)
{
    /*找出每个交叉点坐标*/
    int row = (event->x()- m_margin + m_cellSize / 2)/m_cellSize;  // + m_cellSize / 2 防止向下取整导致误差
    int col = (event->y()- m_margin + m_cellSize / 2)/m_cellSize;

    /*判断鼠标是否在棋盘内*/
    if((row >= 0 && row < m_boardSize) && (col >= 0 && col < m_boardSize) && (m_board[row][col] == EMPTY))
    {
        emit chessClick(row,col);
        /*插入数据*/
//       m_board[row][col] = PLAYER;
//       /*更新棋盘*/
//       update();
    }
    qDebug()<<"点击"<<endl;
}
